package com.wxzz.elearing.livetelecast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.livetelecast.dto.*;
import com.wxzz.elearing.livetelecast.entity.Activity;
import com.wxzz.elearing.livetelecast.entity.PlatformLivetelecast;
import com.wxzz.elearing.livetelecast.entity.PlatformLivetelecastCollect;
import com.wxzz.elearing.livetelecast.entity.WxUserPlatformLivetelecast;
import com.wxzz.elearing.livetelecast.mapper.PlatformLivetelecastCollectMapper;
import com.wxzz.elearing.livetelecast.mapper.WxUserPlatformLivetelecastMapper;
import com.wxzz.elearing.livetelecast.service.IWxUserPlatformLivetelecastService;
import com.wxzz.elearing.livetelecast.service.openfeign.IWxUserApiFeign;
import com.wxzz.elearning.common.aop.Idempotent;
import com.wxzz.elearning.common.dto.FeignWxUserDto;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-02
 */
@Service
public class WxUserPlatformLivetelecastServiceImpl extends ServiceImpl<WxUserPlatformLivetelecastMapper, WxUserPlatformLivetelecast> implements IWxUserPlatformLivetelecastService {

    @Autowired
    private WxUserPlatformLivetelecastMapper wxUserPlatformLivetelecastMapper;

    @Override
    public ReturnVO<List<JgPageVo<WxUserPlatformLivetelecast>>> listWxUserPlatformLivetelecast(WxUserPlatformLivetelecastDto wxUserPlatformLivetelecastDto) {
        PageHelper.startPage(wxUserPlatformLivetelecastDto.getCurPage(), wxUserPlatformLivetelecastDto.getMaxPage());
        QueryWrapper<WxUserPlatformLivetelecast> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("appointment_time");

        if (StringUtils.isNotBlank(wxUserPlatformLivetelecastDto.getEnterpriseName())) {
            queryWrapper.like("enterprise_name", wxUserPlatformLivetelecastDto.getEnterpriseName() + "%");
        }
        if (StringUtils.isNotBlank(wxUserPlatformLivetelecastDto.getCommonCategory())) {
            queryWrapper.like("common_category", wxUserPlatformLivetelecastDto.getCommonCategory() + "%");
        }
        if (StringUtils.isNotBlank(wxUserPlatformLivetelecastDto.getRealName())) {
            queryWrapper.like("real_name", wxUserPlatformLivetelecastDto.getRealName() + "%");
        }
        if (wxUserPlatformLivetelecastDto.getActivityId() != null) {
            queryWrapper.eq("activity_id", wxUserPlatformLivetelecastDto.getActivityId());
        }
        //直播预约列表
        if (wxUserPlatformLivetelecastDto.getPlatformLivetelecastId() != null) {
            queryWrapper.and(qw -> qw.eq("invited_type", 0).or().eq("invited_type", 1));
            queryWrapper.eq("platform_livetelecast_id", wxUserPlatformLivetelecastDto.getPlatformLivetelecastId());
        }
        List<WxUserPlatformLivetelecast> wxUserPlatformLivetelecasts = wxUserPlatformLivetelecastMapper.selectList(queryWrapper);
        for (WxUserPlatformLivetelecast wxUserPlatformLivetelecast : wxUserPlatformLivetelecasts) {
            if (wxUserPlatformLivetelecast.getWxUserId() != null) {
                FeignWxUserDto feignWxUserDto = wxUserApiFeign.selectFeignWxUserDtoByWxUserId(wxUserPlatformLivetelecast.getWxUserId());
                wxUserPlatformLivetelecast.setEnterpriseName(feignWxUserDto.getEnterpriseName());
            }
        }
        return new ReturnVO("直播预约列表查询成功", new JgPageVo<>(new PageInfo(wxUserPlatformLivetelecasts)));
    }

    @Autowired
    private IWxUserApiFeign wxUserApiFeign;

    @Override
    public ReturnVO<List<JgPageVo<WxUserPlatformLivetelecast>>> selectWatchPlatformLivetelecast(WxUserPlatformLivetelecastDto wxUserPlatformLivetelecastDto) {
        PageHelper.startPage(wxUserPlatformLivetelecastDto.getCurPage(), wxUserPlatformLivetelecastDto.getMaxPage());
        QueryWrapper<WxUserPlatformLivetelecast> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("appointment_time");
        queryWrapper.eq("is_watch", 1);
        if (wxUserPlatformLivetelecastDto.getIsReward() != null && wxUserPlatformLivetelecastDto.getIsReward() == 1) {
            queryWrapper.ne("reward_amount", 0);
        }
        if (StringUtils.isNotBlank(wxUserPlatformLivetelecastDto.getEnterpriseName())) {
            queryWrapper.like("enterprise_name", wxUserPlatformLivetelecastDto.getEnterpriseName() + "%");
        }
        if (StringUtils.isNotBlank(wxUserPlatformLivetelecastDto.getCommonCategory())) {
            queryWrapper.like("common_category", wxUserPlatformLivetelecastDto.getCommonCategory() + "%");
        }
        if (StringUtils.isNotBlank(wxUserPlatformLivetelecastDto.getRealName())) {
            queryWrapper.like("real_name", wxUserPlatformLivetelecastDto.getRealName() + "%");
        }
        if (wxUserPlatformLivetelecastDto.getActivityId() != null) {
            queryWrapper.eq("activity_id", wxUserPlatformLivetelecastDto.getActivityId());
        }
        queryWrapper.eq("platform_livetelecast_id", wxUserPlatformLivetelecastDto.getPlatformLivetelecastId());
        List<WxUserPlatformLivetelecast> wxUserPlatformLivetelecasts = wxUserPlatformLivetelecastMapper.selectList(queryWrapper);
        for (WxUserPlatformLivetelecast wxUserPlatformLivetelecast : wxUserPlatformLivetelecasts) {
            if (wxUserPlatformLivetelecast.getWxUserId() != null) {
                FeignWxUserDto feignWxUserDto = wxUserApiFeign.selectFeignWxUserDtoByWxUserId(wxUserPlatformLivetelecast.getWxUserId());
                wxUserPlatformLivetelecast.setEnterpriseName(feignWxUserDto.getEnterpriseName());
            }
        }
        return new ReturnVO("直播观看列表查询成功", new JgPageVo<>(new PageInfo(wxUserPlatformLivetelecasts)));
    }

    @Override
    public ReturnVO<JgPageVo<List<WxUserPlatformLivetelecast>>> adminListInvitedWxUser(AdminListInvitedWxUserDto adminListInvitedWxUserDto) {
        PageHelper.startPage(adminListInvitedWxUserDto.getCurPage(), adminListInvitedWxUserDto.getMaxPage());
        List<WxUserPlatformLivetelecast> wxUserPlatformLivetelecasts = wxUserPlatformLivetelecastMapper.selectList(new LambdaQueryWrapper<WxUserPlatformLivetelecast>()
                .eq(WxUserPlatformLivetelecast::getPlatformLivetelecastId, adminListInvitedWxUserDto.getPlatformLivetelecastId())
                .eq(WxUserPlatformLivetelecast::getType, 0)
                .ne(WxUserPlatformLivetelecast::getInvitationNum, 0)
                .orderByDesc(WxUserPlatformLivetelecast::getInvitationNum));
        for (WxUserPlatformLivetelecast wxUserPlatformLivetelecast : wxUserPlatformLivetelecasts) {
            if (wxUserPlatformLivetelecast.getWxUserId() != null) {
                FeignWxUserDto feignWxUserDto = wxUserApiFeign.selectFeignWxUserDtoByWxUserId(wxUserPlatformLivetelecast.getWxUserId());
                wxUserPlatformLivetelecast.setEnterpriseName(feignWxUserDto.getEnterpriseName());
            }
        }
        return new ReturnVO("排行榜查询成功", new JgPageVo<>(new PageInfo(wxUserPlatformLivetelecasts)));
    }

    @Override
    public ReturnVO<JgPageVo<List<WxUserPlatformLivetelecast>>> listInvitedWxUser(InvitedWxUserDto invitedWxUserDto) {
        PageHelper.startPage(invitedWxUserDto.getCurPage(), invitedWxUserDto.getMaxPage());
        QueryWrapper<WxUserPlatformLivetelecast> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("platform_livetelecast_id", invitedWxUserDto.getPlatformLivetelecastId());
        queryWrapper.eq("invited_wx_user_id", invitedWxUserDto.getWxUserId());
        List<WxUserPlatformLivetelecast> wxUserPlatformLivetelecasts = wxUserPlatformLivetelecastMapper.selectList(queryWrapper);
        for (WxUserPlatformLivetelecast wxUserPlatformLivetelecast : wxUserPlatformLivetelecasts) {
            if (wxUserPlatformLivetelecast.getWxUserId() != null) {
                FeignWxUserDto feignWxUserDto = wxUserApiFeign.selectFeignWxUserDtoByWxUserId(wxUserPlatformLivetelecast.getWxUserId());
                wxUserPlatformLivetelecast.setEnterpriseName(feignWxUserDto.getEnterpriseName());
            }
        }
        return new ReturnVO("排行榜查询成功", new JgPageVo<>(new PageInfo(wxUserPlatformLivetelecasts)));
    }


    @Override
    public ReturnVO<JgPageVo<List<PlatformLivetelecast>>> selectMyWatchPlatformLivetelecast(Long wxUserId, MyWatchPlatformLivetelecastDto myWatchPlatformLivetelecastDto) {
        PageHelper.startPage(myWatchPlatformLivetelecastDto.getCurPage(), myWatchPlatformLivetelecastDto.getMaxPage());
        List<PlatformLivetelecast> platformLivetelecasts = wxUserPlatformLivetelecastMapper.listMyWatchWxUserPlatformLivetelecast(wxUserId, myWatchPlatformLivetelecastDto);
        return new ReturnVO("直播观看列表查询成功", new JgPageVo<>(new PageInfo(platformLivetelecasts)));
    }

    @Autowired
    private PlatformLivetelecastCollectMapper platformLivetelecastCollectMapper;

    @Idempotent
    @Override
    public ReturnVO savePlatformLivetelecastCollect(Long wxUserId, PlatformLivetelecastCollectInsDto platformLivetelecastCollectInsDto) {
        PlatformLivetelecastCollect platformLivetelecastCollect = platformLivetelecastCollectMapper.selectOne(new LambdaQueryWrapper<PlatformLivetelecastCollect>()
                .eq(PlatformLivetelecastCollect::getWxUserId, wxUserId)
                .eq(PlatformLivetelecastCollect::getType, platformLivetelecastCollectInsDto.getType())
                .eq(PlatformLivetelecastCollect::getPlatformLivetelecastId, platformLivetelecastCollectInsDto.getPlatformLivetelecastId()));
        if (platformLivetelecastCollect == null) {
            //如果不存在直接收藏
            PlatformLivetelecastCollect save = new PlatformLivetelecastCollect();
            save.setWxUserId(wxUserId);
            save.setIsCollect(1);
            save.setPlatformLivetelecastId(platformLivetelecastCollectInsDto.getPlatformLivetelecastId());
            save.setType(platformLivetelecastCollectInsDto.getType());
            platformLivetelecastCollectMapper.insert(save);
        } else {
            //反向操作
            platformLivetelecastCollect.setIsCollect(platformLivetelecastCollect.getIsCollect() == 0 ? 1 : 0);
            platformLivetelecastCollectMapper.updateById(platformLivetelecastCollect);
        }
        return new ReturnVO("收藏或取消成功", null);
    }

    @Override
    public ReturnVO<JgPageVo<List<Activity>>> listActivityCollect(Long wxUserId, ActivityCollectDto activityCollectDto) {
        PageHelper.startPage(activityCollectDto.getCurPage(), activityCollectDto.getMaxPage());
        List<Activity> activities = platformLivetelecastCollectMapper.listActivityCollect(wxUserId, activityCollectDto);
        return new ReturnVO("直播收藏列表查询成功", new JgPageVo<>(new PageInfo(activities)));
    }

    @Override
    public ReturnVO<JgPageVo<List<PlatformLivetelecast>>> listPlatformLivetelecastCollect(Long wxUserId, PlatformLivetelecastCollectDto platformLivetelecastCollectDto) {
        PageHelper.startPage(platformLivetelecastCollectDto.getCurPage(), platformLivetelecastCollectDto.getMaxPage());
        List<PlatformLivetelecast> platformLivetelecasts = platformLivetelecastCollectMapper.listPlatformLivetelecastCollect(wxUserId, platformLivetelecastCollectDto);
        return new ReturnVO("直播收藏列表查询成功", new JgPageVo<>(new PageInfo(platformLivetelecasts)));
    }
}
